package com.qyx.pmpucat.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.core.paginate.Page;
import com.qyx.pmpucat.common.result.Result;
import com.qyx.pmpucat.common.result.Results;
import com.qyx.pmpucat.dto.*;
import com.qyx.pmpucat.entity.Question;
import com.qyx.pmpucat.entity.UserAnswer;
import com.qyx.pmpucat.service.UserAnswerService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户答题控制器
 */
@RestController
@RequestMapping("/userAnswer")
@RequiredArgsConstructor
@Validated
public class UserAnswerController {

    private final UserAnswerService userAnswerService;

    /**
     * 提交单个题目的答案并获取下一道题目
     */
    @PostMapping("/submitSingle")
    public Result<Question> submitSingleAnswer(@RequestBody @Valid UserAnswerSingleSubmitDTO submitDTO) {
        Question nextQuestion = userAnswerService.submitSingleAnswer(submitDTO);
        return Results.success(nextQuestion);
    }

    /**
     * 获取用户在指定问卷的最新答题记录
     */
    @GetMapping("/latest")
    public Result<UserAnswerResultDTO> getLatestUserAnswer(@RequestParam Long questionnaireId) {
        Long userId = StpUtil.getLoginIdAsLong();
        UserAnswerResultDTO result = userAnswerService.getLatestUserAnswer(userId, questionnaireId);
        return Results.success(result);
    }

//    /**
//     * 获取评估结果(单题库)
//     */
//    @GetMapping("/evaluation/{userAnswerId}")
//    public Result<String> getEvaluationResult(@PathVariable Long userAnswerId) {
//        String result = userAnswerService.getEvaluationResult(userAnswerId);
//        return Results.success(result);
//    }

    /**
     * 批量获取评估结果
     */
    @GetMapping("/evaluation/batch/{userAnswerGroupId}")
    public Result<List<EvaluationResultBatchDTO>> listEvaluationResult(@PathVariable Long userAnswerGroupId) {
        return Results.success(userAnswerService.listEvaluationResult(userAnswerGroupId));
    }

    /**
     * 下载word评估结果（多题库）
     *
     * @param userAnswerGroupId
     * @return
     */
    @GetMapping("/downLoad-word/batch/{userAnswerGroupId}/{userId}")
    public ResponseEntity<byte[]> downLoadWordBatch(@PathVariable Long userAnswerGroupId,
                                                    @PathVariable Long userId) {
        return userAnswerService.downLoadWordBatch(userAnswerGroupId, userId);
    }

    /**
     * 下载word评估结果（单题库）
     *
     * @param userAnswerGroupId
     * @return
     */
    @GetMapping("/downLoad-word/{userAnswerGroupId}/{userId}")
    public ResponseEntity<byte[]> downLoadWord(@PathVariable Long userAnswerGroupId,
                                               @PathVariable Long userId) {
        return userAnswerService.downLoadWord(userAnswerGroupId, userId);
    }


    /**
     * 获取用户答题记录详情
     */
    @GetMapping("/{id}")
    public Result<UserAnswer> getUserAnswerDetail(@PathVariable Long id) {
        UserAnswer result = userAnswerService.getUserAnswerDetail(id);
        return Results.success(result);
    }

    /**
     * 获取用户答题详情列表
     */
    @GetMapping("/detail/{userAnswerId}")
    public Result<List<UserAnswerDetailResponseDTO>> getUserAnswerDetailList(@PathVariable Long userAnswerId) {
        List<UserAnswerDetailResponseDTO> result = userAnswerService.getUserAnswerDetailList(userAnswerId);
        return Results.success(result);
    }

    /**
     * 分页查询用户答题记录
     */
    @PostMapping("/page")
    public Result<Page<UserAnswerListResponseDTO>> getUserAnswerPage(@RequestBody UserAnswerQueryDTO queryDTO) {
        Page<UserAnswerListResponseDTO> result = userAnswerService.getUserAnswerPage(queryDTO);
        return Results.success(result);
    }

    /**
     * 获取用户在指定问卷的最新答题记录
     */
    @GetMapping("/latest/{userId}/{questionnaireId}")
    public Result<UserAnswerResultDTO> getLatestUserAnswerById(@PathVariable Long userId, @PathVariable Long questionnaireId) {
        UserAnswerResultDTO result = userAnswerService.getLatestUserAnswer(userId, questionnaireId);
        return Results.success(result);
    }

    /**
     * 获取用户答题组列表
     */
    @GetMapping("/group/list")
    public Result<List<UserAnswerGroupResultDTO>> getUserAnswerGroupList(@RequestParam(required = false) Long userId) {
        List<UserAnswerGroupResultDTO> result = userAnswerService.getUserAnswerGroupList(userId);
        return Results.success(result);
    }

    /**
     * 导出用户答题记录
     */
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportUserAnswer(@RequestParam(required = false) LocalDate startTime,
                                                   @RequestParam(required = false) LocalDate endTime) {

        return userAnswerService.exportUserAnswer(startTime,endTime);
    }
} 